package com.tbit.uqbike.webmanager.controller;

import com.tbit.uqbike.enums.DispatchApplyHandleType;
import com.tbit.uqbike.object.pojo.dto.DispatchApplyDealApplyDTO;
import com.tbit.uqbike.object.pojo.vo.DispatchApplyVO;
import com.tbit.uqbike.object.value.LoginInfo;
import com.tbit.common.entity.view.PageResult;
import com.tbit.common.entity.view.Result;
import com.tbit.uqbike.service.business.DispatchApplyLogService;
import com.tbit.uqbike.service.business.DownloadHistoryService;
import com.tbit.uqbike.webmanager.util.csv.entity.RecordDownloadDTO;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * 车辆调度申请
 *
 * @author yee_
 * @version 1.0
 * @date 2023/2/13 16:44
 */
@RestController
@RequestMapping("/dispatchApplyLog")
public class DispatchApplyLogController extends BaseController {

    @Autowired
    private DispatchApplyLogService dispatchLogService;
    @Autowired
    private DownloadHistoryService downloadHistoryService;


    /**
     * 车辆调度申请分页查询
     */
    @GetMapping("/listPage")
    public Result<PageResult<DispatchApplyVO>> listPage(@RequestParam Integer accountId, Integer applyUserId, Integer state,
                                                        Long startTime, Long endTime,
                                                        String  handleType,
                                                        @RequestParam(required = false, defaultValue = "1") Integer page,
                                                        @RequestParam(required = false, defaultValue = "10") Integer size) {
        DispatchApplyHandleType type = DispatchApplyHandleType.getByValue(handleType);
        return dispatchLogService.listPage(accountId, applyUserId, state, type, page, size, startTime, endTime);
    }


    /**
     * 车辆调度申请导出Csv
     */
    @GetMapping("/export")
    public Result<String>  exportCsv(@RequestParam Integer accountId, Integer applyUserId, Integer state,
                          String  handleType,
                          Long startTime, Long endTime) {
        DispatchApplyHandleType type = DispatchApplyHandleType.getByValue(handleType);
        String url = dispatchLogService.exportCsv(accountId, applyUserId, state,type, startTime, endTime);
        if (StringUtils.isNotBlank(url)){
            RecordDownloadDTO downloadDTO = new RecordDownloadDTO();
            downloadDTO.setFileName("车辆调度申请记录");
            downloadDTO.setUrl(url);
            downloadHistoryService.recordSuccess(downloadDTO);
        }
        return Result.success(url);
    }

    /**
     * 车辆调度申请导出excel
     */
    @GetMapping("/exportExcel")
    public Result<Object>  export(@RequestParam Integer accountId, Integer applyUserId, Integer state,
                                     String  handleType,
                                     Long startTime, Long endTime) {
        DispatchApplyHandleType type = DispatchApplyHandleType.getByValue(handleType);
        return dispatchLogService.export(accountId, applyUserId, state,type, startTime, endTime);
    }

    /**
     * 处理审批调度申请
     *
     * @param reviewDTO 。
     * @param request   。
     * @return .
     */
    @PostMapping("/dealWithApply")
    public Result<String> dealWithApply(@RequestBody DispatchApplyDealApplyDTO reviewDTO, HttpServletRequest request) {
        LoginInfo loginUser = getLogin(request);
        reviewDTO.setStockUserId(loginUser.getAccountUserId());
        reviewDTO.setStockUserName(loginUser.getName());
        dispatchLogService.dealWithApply(reviewDTO);
        return Result.success();
    }


}
